
#include "uart.c"
#include "timer.c"
#include <regx52.h>

// 定义出拳类型
#define ROCK 0
#define SCISSORS 1
#define PAPER 2

// LED和按钮定义
sbit player_led = P2^0;    // 玩家胜利LED
sbit system_led = P2^7;    // 系统胜利LED
sbit rock_btn = P1^1;      // 石头按钮
sbit rock_led = P2^1;      // 玩家石头LED
sbit rock_led_s = P2^6;    // 系统石头LED
sbit scissors_btn = P1^2;  // 剪刀按钮
sbit scissors_led = P2^2;  // 玩家剪刀LED
sbit scissors_led_s = P2^5;// 系统剪刀LED
sbit paper_btn = P1^3;     // 布按钮
sbit paper_led = P2^3;     // 玩家布LED
sbit paper_led_s = P2^4;   // 系统布LED
sbit restart_btn = P1^0;   // 重新开始按钮

// 函数声明
void judge(unsigned char user, unsigned char system);
void delay_ms(unsigned int ms);
void clear_all_choice_leds();
void clear_all_leds();
void show_player_choice(unsigned char choice);
void show_system_choice(unsigned char choice);

/**
 * 等待用户出拳，点击按钮代表出拳石头剪刀布。系统随机出拳。
 * 决出胜负，胜利方灯亮。点击按钮重新开始游戏。
 */
void main()
{
    unsigned char user_choice, system_choice;
    unsigned int rand_val;
    bit game_active = 1;  

    // 初始化
    timer0_init();  
    uart_init();    
    clear_all_leds(); 
    
    // 初始化随机数种子
    timer_seed = get_timer0_value();

    while(1)
    {
        if (game_active)
        {
            // 检测石头按钮
            if (rock_btn == 0)
            {
                delay_ms(10);  
                if (rock_btn == 0)
                {
                    user_choice = ROCK;
                    show_player_choice(user_choice); 
                    while(rock_btn == 0); // 等待释放
                    
                    // 生成系统出拳
                    rand_val = timer_rand();
                    system_choice = rand_val % 3;
                    show_system_choice(system_choice); 
                    
                    // 判断胜负
                    judge(user_choice, system_choice);
                    game_active = 0; // 游戏结束
                }
            }
            // 检测剪刀按钮
            else if (scissors_btn == 0)
            {
                delay_ms(10);
                if (scissors_btn == 0)
                {
                    user_choice = SCISSORS;
                    show_player_choice(user_choice);
                    while(scissors_btn == 0);
                    
                    rand_val = timer_rand();
                    system_choice = rand_val % 3;
                    show_system_choice(system_choice);
                    
                    judge(user_choice, system_choice);
                    game_active = 0;
                }
            }
            // 检测布按钮
            else if (paper_btn == 0)
            {
                delay_ms(10);
                if (paper_btn == 0)
                {
                    user_choice = PAPER;
                    show_player_choice(user_choice);
                    while(paper_btn == 0);
                    
                    rand_val = timer_rand();
                    system_choice = rand_val % 3;
                    show_system_choice(system_choice);
                    
                    judge(user_choice, system_choice);
                    game_active = 0;
                }
            }
        }
        else
        {
            // 检测重新开始按钮
            if (restart_btn == 0)
            {
                delay_ms(10);
                if (restart_btn == 0)
                {
                    while(restart_btn == 0); // 等待释放
                    clear_all_leds(); // 熄灭所有LED
                    game_active = 1; // 重新开始游戏
                }
            }
        }
    }
}

void judge(unsigned char user, unsigned char system)
{
    // 平局 - 所有灯灭
    if (user == system)
    {
        player_led = 1;
        system_led = 1;
        return;
    }
    
    // 玩家胜利条件
    if ((user == ROCK && system == SCISSORS) ||
        (user == SCISSORS && system == PAPER) ||
        (user == PAPER && system == ROCK))
    {
        player_led = 0;  
        system_led = 1;
    }
    // 系统胜利条件
    else
    {
        player_led = 1;
        system_led = 0;  
    }
}


void show_player_choice(unsigned char choice)
{
    clear_all_choice_leds(); 
    
    switch(choice) {
        case ROCK:
            rock_led = 0; 
            break;
        case SCISSORS:
            scissors_led = 0; 
            break;
        case PAPER:
            paper_led = 0; 
            break;
    }
}


void show_system_choice(unsigned char choice)
{

    switch(choice) {
        case ROCK:
            rock_led_s = 0; 
            break;
        case SCISSORS:
            scissors_led_s = 0; 
            break;
        case PAPER:
            paper_led_s = 0; 
            break;
    }
}


void clear_all_choice_leds()
{
    
    rock_led = 1;
    scissors_led = 1;
    paper_led = 1;
    
   
    rock_led_s = 1;
    scissors_led_s = 1;
    paper_led_s = 1;
}


void clear_all_leds()
{
    clear_all_choice_leds();
    player_led = 0;
    system_led = 0;
}


void timer0_isr() interrupt 1
{
    // 空实现，仅用于维持定时器运行
}


void uart_isr() interrupt 4
{
    if (RI)
    {
        RI = 0; 
    }
    if (TI)
    {
        TI = 0; 
    }
}
